package com.haixee.parkdemo.custom.service.impl;

import com.haixee.parkdemo.common.context.WebContextHolder;
import com.haixee.parkdemo.common.exception.BusinessException;
import com.haixee.parkdemo.common.feign.auth.AuthRPCService;
import com.haixee.parkdemo.common.response.CommonResEnum;
import com.haixee.parkdemo.custom.dao.CustomCustomMapper;
import com.haixee.parkdemo.custom.dao.CustomMapper;
import com.haixee.parkdemo.custom.model.Custom;
import com.haixee.parkdemo.custom.service.CustomService;
import com.haixee.parkdemo.custom.service.UserScopeService;
import com.haixee.parkdemo.data_acl.mybatis.DataAclHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class CustomServiceImpl implements CustomService {

    @Autowired
    private CustomMapper customMapper;

    @Autowired
    private CustomCustomMapper customCustomMapper;

    @Autowired
    private UserScopeService userScopeService;

    @Autowired
    private AuthRPCService authService;

    @Override
    @Transactional
    public void addCustom(String name, String phone, String company, String address, Integer optUid) {

        Integer userVer = userScopeService.insertBeforeCheck(WebContextHolder.getUid());

        Custom custom = new Custom();
        custom.setName(name);
        custom.setPhone(phone);
        custom.setCompany(company);
        custom.setAddress(address);
        custom.setOptUid(optUid);
//        custom.setOptJob(WebContextHolder.getAuth().getJobId());
//        custom.setOptDept(WebContextHolder.getAuth().getDeptId());
        custom.setTenantId(WebContextHolder.getAuth().getTenantId());
        custom.setUidVersion(userVer);
        customMapper.insertSelective(custom);
    }

    @Override
    public void updateCustom(Integer id, String name, String phone, String company, String address, Integer optUid) {

        Custom custom = new Custom();
        custom.setId(id);
        custom.setPhone(phone);
        custom.setName(name);
        custom.setCompany(company);
        custom.setAddress(address);

        List<Integer> scope = userScopeService.getScope(optUid, WebContextHolder.getAuth().getUnderDeptList());
        if (customCustomMapper.selectByScope(scope) == null) {
            throw new BusinessException(CommonResEnum.PERMISSION_DENIED);
        }

//        DataAclHelper.start();
        int updateRow = customMapper.updateByPrimaryKeySelective(custom);
        if (updateRow == 0) {
            throw new BusinessException(CommonResEnum.PERMISSION_DENIED);
        }
    }

    @Override
    public List<Custom> getCustomList(Integer uid) {

//        DataAclHelper.start();
        List<Integer> scope = userScopeService.getScope(uid, WebContextHolder.getAuth().getUnderDeptList());
        return customCustomMapper.selectByScope(scope);
    }

    @Override
    public void assign(Integer id, Integer optUid) {

        Custom custom = new Custom();
        custom.setId(id);
        custom.setOptUid(optUid);
        customMapper.updateByPrimaryKeySelective(custom);
    }

}
